<?php

class class_textcode {
	var $convmap = array();
	
	function textcode() {
		$this->convmap = array(
			0xe1,0xe1,0x0,0xe1,0xc1,0xc1,0x0,0xc1,0xe0,0xe0,0x0,0xe0,
			0xc0,0xc0,0x0,0xc0,0x1ea3,0x1ea3,0x0,0x1ea3,0x1ea2,0x1ea2,0x0,0x1ea2,
			0xe3,0xe3,0x0,0xe3,0xc3,0xc3,0x0,0xc3,0x1ea1,0x1ea1,0x0,0x1ea1,
			0x1ea0,0x1ea0,0x0,0x1ea0,0x103,0x103,0x0,0x103,0x102,0x102,0x0,0x102,
			0x1eaf,0x1eaf,0x0,0x1eaf,0x1eae,0x1eae,0x0,0x1eae,0x1eb1,0x1eb1,0x0,0x1eb1,
			0x1eb0,0x1eb0,0x0,0x1eb0,0x1eb3,0x1eb3,0x0,0x1eb3,0x1eb2,0x1eb2,0x0,0x1eb2,
			0x1eb5,0x1eb5,0x0,0x1eb5,0x1eb4,0x1eb4,0x0,0x1eb4,0x1eb7,0x1eb7,0x0,0x1eb7,
			0x1eb6,0x1eb6,0x0,0x1eb6,0xe2,0xe2,0x0,0xe2,0xc2,0xc2,0x0,0xc2,
			0x1ea5,0x1ea5,0x0,0x1ea5,0x1ea4,0x1ea4,0x0,0x1ea4,0x1ea7,0x1ea7,0x0,0x1ea7,
			0x1ea6,0x1ea6,0x0,0x1ea6,0x1ea9,0x1ea9,0x0,0x1ea9,0x1ea8,0x1ea8,0x0,0x1ea8,
			0x1eab,0x1eab,0x0,0x1eab,0x1eaa,0x1eaa,0x0,0x1eaa,0x1ead,0x1ead,0x0,0x1ead,
			0x1eac,0x1eac,0x0,0x1eac,0xe9,0xe9,0x0,0xe9,0xc9,0xc9,0x0,0xc9,
			0xe8,0xe8,0x0,0xe8,0xc8,0xc8,0x0,0xc8,0x1ebb,0x1ebb,0x0,0x1ebb,
			0x1eba,0x1eba,0x0,0x1eba,0x1ebd,0x1ebd,0x0,0x1ebd,0x1ebc,0x1ebc,0x0,0x1ebc,
			0x1eb9,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ef5,0x1ef4,0x1ef4,0x0,0x1ef4,
			0x111,0x111,0x0,0x111,0x110,0x110,0x0,0x110,
		);
	}
	
	function RemoveAccent($strAccent = "", $sepchar = "") {
		
		$strfrom 	= " Á À Ả Ã Ạ Â Ấ Ầ Ẩ Ẫ Ậ Ă Ắ Ằ Ẳ Ẵ Ặ Đ É È Ẻ Ẽ Ẹ Ê Ế Ề Ể Ễ Ệ Í Ì Ỉ Ĩ Ị Ó Ò Ỏ Õ Ọ Ơ Ớ Ờ Ở Ỡ Ợ Ô Ố Ồ Ổ Õ Ộ Ú Ù Ủ Ũ Ụ Ư Ứ Ừ Ử Ữ Ự Ý Ỳ Ỷ Ỹ Ỵ ";
		$strto		= " A A A A A A A A A A A A A A A A A D E E E E E E E E E E E I I I I I O O O O O O O O O O O O O O O O O U U U U U U U U U U U Y Y Y Y Y ";
		
		$strfrom 	.= "á à ả ã ạ â ấ ầ ẩ ẫ ậ ă ắ ằ ẳ ẵ ặ đ é è ẻ ẽ ẹ ê ế ề ể ễ ệ í ì ỉ ĩ ị ó ò ỏ õ ọ ơ ớ ờ ở ỡ ợ ô ố ồ ổ ỗ ộ ú ù ủ ũ ụ ư ứ ừ ử ữ ự ý ỳ ỷ ỹ ỵ";
		$strto		.= "a a a a a a a a a a a a a a a a a d e e e e e e e e e e e i i i i i o o o o o o o o o o o o o o o o o u u u u u u u u u u u y y y y y";

		$fromarr = explode(" ", $strfrom);
		$toarr = explode(" ", $strto);
		
		$dicarr = array();
		for($i=1;$i<count($fromarr);$i++) {
			$dicarr[$fromarr[$i]] = $toarr[$i];
		}

		$strNoAccent = strtr($strAccent,$dicarr);
		
		// Remove html special character
		$htmlspecs = "acute circ grave tilde nbsp";
		$htmlspecscharArr = explode(" ",$htmlspecs);
		$strNoAccent = str_replace($htmlspecscharArr,"",$strNoAccent);
		
		// Remove special character
		$specialchar = ", . ? / : ! < > & * ^ % $ # @ ; ' ( ) { } [ ] + - 39";
		$specialcharArr = explode(" ",$specialchar);
		$strNoAccent = str_replace($specialcharArr,"",$strNoAccent);
		
		if($sepchar) {
			$strNoAccent = str_replace(" ",$sepchar, $strNoAccent);
			$strNoAccent = str_replace($sepchar.$sepchar,$sepchar,$strNoAccent);
		}
			
		return $strNoAccent;
	}
	
	function UniToDecimal($str) {
		return mb_encode_numericentity($str, $this->convmap, "UTF-8");
	}
	
	function DecimalToUni($str) {
		return mb_decode_numericentity($str, $this->convmap, "UTF-8");
	}
	
	function CutString($string = "", $num = 20){	
	    if(strlen($string) > $num)
	    {
	    	$result = substr($string,0,$num); //cut string with limited number
	    	$position = strrpos($result," "); //find position of last space
	    	if($position)
	    		$result = substr($result,0,$position); //cut string again at last space if there are space in the result above    	
	    	$result .= ' ...';
	    }
	    else {
	    	$result = $string;    	
	    }    
	    return $result;
	}
}